<template>
  <div class="main-content no-padding">
    <div class="container-fluid">
      <div class="panel">
        <div class="panel-body">
          <el-row>
            <el-col :span="10" :offset="7">
              <el-input placeholder="编码或名称" v-model="search_text" class="input-with-select">
                <template #append>
                  <el-button icon="el-icon-search" @click="search"></el-button>
                </template>
              </el-input>
            </el-col>
            <el-col :span="7">
              <el-button class="pull-right" @click="dialogAddVisible=true">添加规则</el-button>
            </el-col>
          </el-row>
        </div>
      </div>

      <div class="panel">
        <el-table
          :data="tableData"
          stripe
          style="width: 100%">
          <el-table-column type="expand">
            <template #default="props">
              <pre>
                {{ props.row.data }}
              </pre>
            </template>
          </el-table-column>
          <el-table-column
            width="50"
            label="ID"
            prop="id">
          </el-table-column>
          <el-table-column
            width="200"
            label="名称"
            prop="name">
          </el-table-column>
          <el-table-column
            width="100"
            label="版本"
            prop="version">
          </el-table-column>
          <el-table-column
            width="120"
            :formatter="formatterType"
            label="规则类型"
            prop="type">
          </el-table-column>
          <el-table-column
            label="描述"
            prop="description">
          </el-table-column>
          <el-table-column fixed="right" width="150" label="操作">
            <template #default="scope">
              <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          v-model:current-page="page.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          v-model:page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          v-model:total="page.total">
        </el-pagination>
      </div>
    </div>


    <el-dialog
      title="添加规则"
      v-model="dialogAddVisible"
      width="50%">
      <el-form label-position="left" label-width="120px" :model="rulesParam">
        <el-form-item label="名称">
          <el-input v-model="rulesParam.name" placeholder="一个名称，方便查看用的"></el-input>
        </el-form-item>
        <el-form-item label="版本">
          <el-input v-model="rulesParam.version" placeholder="版本号"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="rulesParam.executeRuleType" placeholder="请选择" style="width: 100%">
            <el-option label="JAVA_RULE" :value="1"></el-option>
            <el-option label="JYTHON_RULE" :value="2"></el-option>
            <el-option label="PYTHON_RULE" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="规则执行类">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-input v-model="rulesParam.executeRule" placeholder="对应的要执行的规则类（注意不要写错）"></el-input>
            </el-col>
            <el-col :span="12">
              <el-input v-model="rulesParam.executeRuleVersion" placeholder="对应的要执行的规则类的版本（注意不要写错）"></el-input>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="计算参数">
          <el-select
            style="width: calc(100% - 80px)"
            v-model="dynamicSelectingRuleParams"
            multiple
            filterable
            remote
            reserve-keyword
            placeholder="请输入关键词"
            :remote-method="remoteMethod"
            :loading="loading">
            <el-option
              v-for="item in dynamicRuleParams"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-button class="pull-right" @click="addRulesParam">添加</el-button>
        </el-form-item>
        <el-form-item label="">
          <el-tag
            :key="tag"
            v-for="tag in dynamicSelectedRuleParams"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)">
            {{ tag }}
          </el-tag>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="rulesParam.description"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogAddVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRulesRule">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

import {fetchRuleCalcParamConfig} from '@/api/engine/rulesParamConfig'
import {addBaseRule, fetchBaseRuleConfig} from "@/api/engine/rulesRuleConfig";

export default {
  name: "RulesRule",
  data() {
    return {
      dynamicRuleParams: [],
      dynamicSelectedRuleParams: [],
      dynamicSelectingRuleParams: [],

      loading: false,
      dialogAddVisible: false,
      rulesParam: {
        name: "",
        version: "",
        executeRuleType: 1,
        executeRule: "",
        executeRuleVersion: "",
        ruleParam: "",
        description: "",
      },
      page: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      search_text: '',
      tableData: [],
    };
  },
  methods: {
    remoteMethod(query) {
      let that = this
      if (query !== '') {
        that.loading = true;
        fetchRuleCalcParamConfig(1, 100).then(response => {
          that.dynamicRuleParams = []
          this.dynamicRuleParams = response.data.dataList.map(item => {
            return {value: item.name, label: item.name};
          });
          that.loading = false;
        })
      } else {
        this.dynamicRuleParams = [];
      }
    },
    handleClose(tag) {
      this.dynamicSelectedRuleParams.splice(this.dynamicSelectedRuleParams.indexOf(tag), 1);
    },
    handleSizeChange() {
      this.search()
    },
    handleCurrentChange() {
      this.search()
    },
    formatterType(row) {
      let label = ""
      switch (row.executeRuleType) {
        case 1:
          label = "JAVA_RULE"
          break
        case 2:
          label = "JYTHON_RULE"
          break
        case 3:
          label = "PYTHON_RULE"
          break
        default:
          label = "不知道"
      }
      return label
    },
    addRulesParam() {
      if (this.dynamicSelectingRuleParams.length === 0) {
        return;
      }

      for (const tag of this.dynamicSelectingRuleParams) {
        if (this.dynamicSelectedRuleParams.indexOf(tag) < 0) {
          this.dynamicSelectedRuleParams.push(tag)
        }
      }
      this.dynamicSelectingRuleParams = []
    },
    addRulesRule() {
      let that = this
      that.rulesParam.ruleParam = this.dynamicSelectedRuleParams.join(",")
      addBaseRule(that.rulesParam).then(() => {
        that.dialogAddVisible = false
        that.search()
        that.ruleParam = {
          name: "",
          version: "",
          executeRuleType: 1,
          executeRule: "",
          executeRuleVersion: "",
          ruleParam: "",
          description: "",
        }
      })
    },

    search: function () {
      let that = this
      fetchBaseRuleConfig(this.page.currentPage, that.page.pageSize).then(response => {
        that.tableData = response.data.dataList
        that.page.total = response.data.totalCount
      })
    },
  },
  mounted() {
    this.search()
  }
}
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
</style>
